﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLang
{
    /// <summary>
    /// 词法分析器上下文对象
    /// </summary>
    public class LContext
    {
        #region 数据属性
        public string CurrentState { get; set; }
        public char CurrentChar { get; private set; }
        public char NextChar { get; private set; }

        public string Source { get; private set; }

        public int Position { get; private set; }
        #endregion

        #region 构造函数
        public LContext() {
        }
        #endregion

        #region 公开方法
        /// <summary>
        /// 加载源代码
        /// </summary>
        /// <param name="src"></param>
        public void Load(string src)
        {
            this.Source = src;
            this.Position = -1;
            this.CurrentState = "Initial";
        }

        /// <summary>
        /// 读取一个字符
        /// </summary>
        /// <returns></returns>
        public bool GetChar()
        {
            if (string.IsNullOrEmpty(Source) || Position >= Source.Length - 1) {
                CurrentChar = '\0';
                NextChar = '\0';
                Position++;
                return false;
            }

            Position++;
            CurrentChar = Source[Position];
            if (Position >= Source.Length-1) NextChar = '\0';
            else NextChar = Source[Position + 1];

            return true;
        }

        /// <summary>
        /// 设置游标的位置
        /// </summary>
        /// <param name="pos"></param>
        public void SetPosition(int pos)
        {
            Position = pos;
        }
        #endregion
    }
}
